#!/bin/sh
OTA_PATH=/usr/bin/gateway.update
EXE_PATH=/usr/bin/gateway
BAK_PATH=/usr/bin/gateway.bak
start() {
    # 检测ota
    if [ -f $OTA_PATH ];then
        mv $EXE_PATH $BAK_PATH
        mv $OTA_PATH $EXE_PATH
        chmod +x $EXE_PATH
    fi
	start-stop-daemon -S -b -m -q -p /var/run/gateway.pid \
		--exec /usr/bin/gateway -- daemon
	[ $? = 0 ] && echo "OK" || echo "FAIL"
}
stop() {
	printf "Stopping gateway: "
	start-stop-daemon -K -q -p /var/run/gateway.pid
	[ $? = 0 ] && echo "OK" || echo "FAIL"
}
restart() {
	stop
	start
}

case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart|reload)
  	restart
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?
